package com.hmdp.learn;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class UserLoginFactory implements ApplicationContextAware {

    //存储：type(登录类型参数)和对应的登录策略类
    private static Map<String, UserGranter> typeStrategyMap = new ConcurrentHashMap<>();

    @Autowired
    private LoginTypeConfig loginTypeConfig;

    /**
     * 为了拿到bean工厂，通过bean工厂获取指定名称的策略对象（bean）
     * 然后将type(登录类型参数)和对应的登录策略类存入map集合
     * @param applicationContext the ApplicationContext object to be used by this object
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//        loginTypeConfig.getConfigMap().forEach((type, beanName) -> {
//            typeStrategyMap.put(type, (UserGranter) applicationContext.getBean(beanName));
//        });
    }

    /**
     * 根据参数返回不同的策略类
     */
    public UserGranter getGranter(String type) {
        return typeStrategyMap.get(type);
    }
}
